package com.coco.group.config.redisconfig;


import com.coco.common.constant.Constant;
import com.coco.group.feign.AlipayFeign;
import com.coco.group.mapper.GroupBuyRecordMapper;
import com.coco.group.pojo.TbGroupBuyRecord;
import com.coco.group.pojo.TbGroupOrder;
import com.coco.group.service.GroupBuyHelpService;
import com.coco.group.service.GroupBuyRecordService;
import com.coco.group.service.GroupOrderService;
import com.coco.group.vo.GroupHelpAndUser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author HanSama
 * @date 2021年07月19日 9:06
 */
@Component
public class RedisListener extends KeyExpirationEventMessageListener {


    @Autowired
    private GroupOrderService orderService;
    @Autowired
    private GroupBuyHelpService helpService;
    @Autowired
    private GroupBuyRecordService recordService;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Autowired
    private RedisTemplate redisTemplate;


    public RedisListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 获取过期的key
        String expiredKey = message.toString();
        // 判断是否是想要监听的过期key
        if (expiredKey.contains(Constant.OrderKeyConstant.WITH_GROUP_ORDER_KEY)) {
            String[] split = expiredKey.split(Constant.OrderKeyConstant.SUB_KEY);
            //获取货期的订单id
            String id = split[Constant.OrderKeyConstant.FIRST_KEY];
            //查询过期的订单
            TbGroupOrder order = orderService.findByOrderId(id);
            //获得订单中的sku
            Long itemId = order.getItemId();
            //删除过期订单
            orderService.deleteGroupOrderById(id);
            Map itemMap = redisTemplate.opsForHash().entries("group_goods:" + itemId);
            //回滚redis中的sku商品库存
            Integer limitResidue = (Integer) itemMap.get("limitResidue");
            limitResidue +=1;
            itemMap.put("limitResidue",limitResidue);
            redisTemplate.opsForHash().putAll("group_goods:" + itemId,itemMap);
            //回滚Redis中的用户参与次数
            Map userGoodsNum = redisTemplate.opsForHash().entries("group_goods_num:" + itemId);
            int o = (int) userGoodsNum.get("userId:" + order.getUserId());
            userGoodsNum.put("userId:" + order.getUserId(),o-1);
            redisTemplate.opsForHash().putAll("group_goods_num:" + itemId,userGoodsNum);
        }

        /**
         * 监听拼团记录过期
         */
        if (expiredKey.contains(Constant.GroupBuyConstant.REDIS_GROUP_RECORD)){
            String[] split = expiredKey.split(Constant.OrderKeyConstant.SUB_KEY);
            //获取过期的拼团邀请码
            String code = split[Constant.OrderKeyConstant.FIRST_KEY];
            //查询该条拼团记录
            TbGroupBuyRecord record = recordService.findByInvitationCode(code);
            //获取该团的成员
            List<GroupHelpAndUser> andUsers = helpService.findBuyHelp(record.getId());
            //向mq发起退款消息
            rabbitTemplate.convertAndSend(Constant.GroupBuyConstant.RETURN_MONEY,andUsers);
            Map itemMap = redisTemplate.opsForHash().entries("group_goods:" + record.getGroupBuyItemId());
            //添加redis中的商品库存
            Integer limitResidue = (Integer) itemMap.get("limitResidue");
            limitResidue +=andUsers.size();
            itemMap.put("limitResidue",limitResidue);
            redisTemplate.opsForHash().putAll("group_goods:" + record.getGroupBuyItemId(),itemMap);
        }
    }


}
